<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*,java.text.*,
com.brother.model.*,com.ibatis.common.util.PaginatedList,com.brother.service.*,com.brother.*
,com.brother.util.DateFormatter,com.brother.database.DataBase,com.brother.util.*"   pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ include file="international.jsp" %> 
<jsp:useBean id="report" class="com.brother.model.Report"></jsp:useBean>
<jsp:useBean id="reportService" class="com.brother.service.ReportService"></jsp:useBean>
<jsp:setProperty property="*" name="report"/>
<%
String trackerId = request.getParameter("trackerid");

com.brother.model.Customer user = (com.brother.model.Customer)session.getAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
if(user == null)
	return;
TrackerService tks = new TrackerService();
List<Tracker> trackers = tks.findUserTracker(user.getUserid());

String pageNo = request.getParameter("pageNo");
if(pageNo == null || !pageNo.matches("^\\d*$")){
	pageNo = "0";
}



report.setUserId(user.getUserid());	 
report.setOrderCondition("order by createtime asc");
SimpleDateFormat formatCN = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // 
report.setCreateTimeFrom(DateFormatter.toEastEight(formatCN.parse(report.getCreateTimeFrom()), user.getTimeZoneOffSet(), "yyyy-MM-dd HH:mm"));
report.setCreateTimeTo(DateFormatter.toEastEight(formatCN.parse(report.getCreateTimeTo()), user.getTimeZoneOffSet(), "yyyy-MM-dd HH:mm"));

 
PaginatedList pageData = reportService.findData("0", (Integer.parseInt(pageNo)+1)*report.getPageSize(),report);
int totalSize = ((MyPaginatedList)pageData).getTotalSize();//reportService.findDataSize(report.getPageSize(),report);
int totalPages = totalSize / report.getPageSize() + 1;



String dateFormat = "MM-dd";
if(session.getAttribute("locale").toString().equals("en")){
	dateFormat = "dd/MM";
}else if(session.getAttribute("locale").toString().equals("pt")){
	dateFormat = "dd/MM";
}

DateFormatter localFormat = new DateFormatter(); 
localFormat.setConfig(user.getTimeZoneOffSet(), dateFormat+" HH:mm:ss");

DateFormatter format = new DateFormatter(); 
format.setConfig(user.getTimeZoneOffSet(), dateFormat+" HH:mm:ss");

RealDataService rds = new RealDataService();

session.setAttribute(Constants.REPORT_SEARCH_PARAMETERS , report);
double mileage = 0f;
LatLngPoint to = new LatLngPoint();
for(int i=0; i< pageData.size(); i++){
	Report from = (Report)pageData.get(i);
	if(i > 0)
	mileage =  mileage + Distance.distance(new LatLngPoint(from.getSignedLat(), from.getSignedLng())
	, to);
	to = new LatLngPoint(from.getSignedLat(), from.getSignedLng());
}


//pageData.gotoPage(Integer.parseInt(pageNo));
//Iterator<Report> datas = pageData.iterator();   

int start = (Integer.parseInt(pageNo))*report.getPageSize();
int end = (Integer.parseInt(pageNo)+1)*report.getPageSize();

System.out.println("totalSize="+totalSize+", pages="+totalPages+"  data size="+pageData.size()
		+"  coutn size="+((Integer.parseInt(pageNo)+1)*report.getPageSize())+" pageNo="+pageNo+
		"  start="+start+"  end="+end+"  trackerId="+trackerId+"  trackerId2="+report.getTrackerid());

if(pageData.size() < end){
	end = pageData.size();
}

%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
﻿<link href="style.css" media="all" type="text/css" rel="stylesheet"/>
<style>
table tr td
{
	font-size:10px;
}
</style>
</head>
<body style="margin: 0px;">



<script>

var g_oldline = -1;
window.parent.beginLoadData();
</script>
<table cellspacing="0" cellpadding="4" border="0">
	<tbody><tr bgcolor="#efefef">
		<th width="80"><fmt:message key="history.search.time"/>:</th>
		<th width="50"><fmt:message key="location.lat"/>:</th>
		<th width="50"><fmt:message key="location.lng"/>:</th>
		<th><fmt:message key="report.speed"/>:</th>
	</tr>

<%
int rowIndex = 0;//Integer.parseInt(pageNo) * report.getPageSize();
for(int i=start; i < end; i++){
	Report data = (Report)pageData.get(i);
	data.setTraceDateTime(data.getCreateTime());
	String gpsData = rds.getDataString(data);
	gpsData = gpsData + "~"+(data.getAddress() == null ?"":data.getAddress());
%>
	<script>window.parent.addGPSData("<%=gpsData%>");</script>
	<tr>
		<td bgcolor="#efefef" style="padding: 0pt; height: 1px;" colspan="4"/>
	</tr>
	<tr onmouseout="javascript:if (<%=rowIndex%> == g_oldline){this.style.background='#8bc1ed'}else{this.style.background='#ffffff'}; this.style.color='#000000'" onmousemove="javascript:this.style.background='#4594dd'; this.style.color='#ffffff';" style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; color: rgb(0, 0, 0); -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" onclick="javascript:window.parent.setFocusOnMap(<%=rowIndex%>, true);" name="line<%=rowIndex%>" id="line<%=rowIndex%>">
		<td nowrap="nowrap"><b><%=localFormat.format(data.getCreateTime())%></b></td>
		<td><%=data.getLat()%></td>
		<td><%=data.getLng()%></td>
		<td align="center"><%=data.getSpeed()%></td>
	</tr>
	<!-- javascript:window.parent.showPopWin('gps_comment.jsp?reportId=<%=data.getReportId()%>', 400, 300) -->
		<tr height="12" onclick="" style="margin-bottom: 5px;">
		<td style="border-color: rgb(255, 159, 85); border-width: 1px; border-left: 1px solid rgb(255, 159, 85); padding-left: 10px; color: rgb(128, 128, 128);" colspan="4"><%=data.getAddress() == null ?"":data.getAddress()%><br/></td>
	</tr>
<%
rowIndex +=1;
} %>
	

	</tbody></table>
<script>

function setActiveToLine(line)
{
	//console.log(line);
	if (g_oldline != -1)
	{
		var obj = document.getElementById("line" + g_oldline);
		if (obj)
		{
			obj.style.background = '#ffffff';
		}
	}
	
	if (line != -1)
	{
		var obj = document.getElementById("line" + line);
		//console.log(line,"  ",obj);
		if (obj)
		{
			obj.style.background = '#8bc1ed';// '#ff8935';
			// obj.focus();
			if (obj.offsetTop - document.body.scrollTop > document.body.offsetHeight - 20 ||
				obj.offsetTop < document.body.scrollTop)
			{
				document.body.scrollTop = obj.offsetTop - 10;
			}
		}
	}	
	
	g_oldline = line;
}
function getActiveLine()
{
	return g_oldline;
}
</script>

<script>window.parent.endLoadData();</script>

<script>
	// window.parent.setFocusOnMap(0, false);
	window.parent.setPageInfo(<%=Integer.parseInt(pageNo)+1%>, <%=totalSize%>, <%=report.getPageSize()%>);
	window.parent.setHistoryMileage(<%=mileage%>);
</script>
</body></html>
<%
DataBase.closeThreadConnection(); 
%>